package com.jinzhi.jzweb.controller;



import java.util.*;

import cn.hutool.core.date.DateTime;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.JazSysProjectDO;
import com.jinzhi.jzweb.service.JazSysProjectService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 服务项目管理
 * </pre>
 * <small> 2023-04-18 08:53:01 | Long</small>
 */
@Controller
@RequestMapping("/jzweb/jazSysProject")
public class JazSysProjectController extends BaseController {
	@Autowired
	private JazSysProjectService jazSysProjectService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:jazSysProject:jazSysProject")
	String JazSysProject(){
	    return "jzweb/jazSysProject/jazSysProject";
	}

	@Log("项目启动禁用")
	@ResponseBody
	@PostMapping("/condition")
	@RequiresPermissions("jzweb:jazSysProject:edit")
	public Result<String> condition( JazSysProjectDO jazSysProject){
		JazSysProjectDO jazSysProjectDO = jazSysProjectService.selectById(jazSysProject.getId());
		//启动
		if(jazSysProject.getStatus().equals(0)){
			jazSysProjectDO.setStatus(0);
		}
		//禁用
		if(jazSysProject.getStatus().equals(1)){
			jazSysProjectDO.setStatus(1);
		}
		boolean b = jazSysProjectService.updateById(jazSysProjectDO);
		return b ? Result.ok() : Result.fail();
	}

	@Log("查询项目列表")
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:jazSysProject:jazSysProject")
	public Result<Page<JazSysProjectDO>> list(JazSysProjectDO jazSysProjectDTO,String current,String size){
		Page<JazSysProjectDO> page1 = new Page<JazSysProjectDO>(Integer.valueOf(current),Integer.valueOf(size));
		Wrapper<JazSysProjectDO> wrapper = new EntityWrapper<JazSysProjectDO>().orderBy("sort",true).orderBy("id", false);
		// 查询列表数据
		Page<JazSysProjectDO> page = jazSysProjectService.selectPage(page1, wrapper);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			JazSysProjectDO obj = (JazSysProjectDO) page.getRecords().get(i);
			obj.setCreateTime(DateTime.of(obj.getCreateTime()));
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}

	@Log("进入新增项目页面")
	@GetMapping("/add")
	@RequiresPermissions("jzweb:jazSysProject:add")
	String add(){
	    return "jzweb/jazSysProject/add";
	}

	@Log("新增项目")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:jazSysProject:add")
	public Result<String> save( JazSysProjectDO jazSysProject){
		//设置项目创建人id、创建时间
		jazSysProject.setSysUserid(getUserId());
		jazSysProject.setCreateTime(new DateTime());
//		//项目默认启动
//		jazSysProject.setStatus(0);
		jazSysProjectService.insert(jazSysProject);
		return Result.ok();
	}

	@Log("进入编辑项目页面")
	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:jazSysProject:edit")
	String edit(@PathVariable("id") Long id,Model model){
		JazSysProjectDO jazSysProject = jazSysProjectService.selectById(id);
		model.addAttribute("jazSysProject", jazSysProject);
	    return "jzweb/jazSysProject/edit";
	}
	
	@Log("编辑并更新项目")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:jazSysProject:edit")
	public Result<String>  update( JazSysProjectDO jazSysProject){
		//设置项目更新时间
		jazSysProject.setUpdateTime(new DateTime());
		boolean update = jazSysProjectService.updateById(jazSysProject);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除单个项目")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:jazSysProject:remove")
	public Result<String>  remove( Long id){
		jazSysProjectService.deleteById(id);
        return Result.ok();
	}

}
